package com.lb.statck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lb.statck.mapper.category.ProductMapper;
import com.lb.statck.mapper.order.OrderMapper;
import com.lb.statck.mapper.orderDetailMapper;
import com.lb.statck.mapper.shopcart.ShopcartMapper;
import com.lb.statck.pojo.dto.OrderDTO;
import com.lb.statck.pojo.dto.OrderQueryDto;
import com.lb.statck.pojo.dto.ShopDto;
import com.lb.statck.pojo.vo.OrderVo;
import com.lb.statck.pojo.vo.ProductVo;
import com.lb.statck.pojo.vo.ShopCartVO;
import com.lb.statck.pojo.vo.orderDetailVo;
import com.lb.statck.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class orderserviceimpel extends ServiceImpl<OrderMapper, OrderVo> implements OrderService {
    @Autowired
    private OrderMapper ordermapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ShopcartMapper shopcartMapper;
    @Autowired
    private orderDetailMapper orderDetailMapper1;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<OrderVo> getListOrder(int id) {
        List<OrderVo> orderVos = ordermapper.orderVoList(id);
     return orderVos;
    }

    @Override
    @Transactional
    public String insertOrder(OrderDTO orderVo) {
      //  String code = orderVo.getProductCode();
     //   ProductVo productVo = new ProductVo();
      //  QueryWrapper<ProductVo> wrapper = new QueryWrapper<>();
       // wrapper.eq("product_code", code);
      //  productVo = productMapper.selectOne(wrapper);
    //    BigDecimal price = productVo.getPrice();
   //     String name = productVo.getName();
   //     orderVo.setStorename(name);
   //     orderVo.setPrice(price);
        //生成订单
        LocalDateTime now = LocalDateTime.now();
        String data = now.toString();
        String randomOrderNumber = UUID.randomUUID().toString().replace("-", "");
        String payOrder=data;
        payOrder = payOrder.replaceAll("[^0-9]", "");

        orderVo.setOrderId(payOrder);

        orderVo.setStatus(1);
        ShopDto shopDto = new ShopDto();
        shopDto.setUserId(Integer.parseInt(orderVo.getUserid()));
        List<ShopCartVO> shopCartVOS = shopcartMapper.ListShopCart(shopDto);
        List<orderDetailVo> orderDetailVos = new ArrayList<>();
        BigDecimal totalPrice = new BigDecimal(2);
        for (ShopCartVO shopCartVO : shopCartVOS) {
            orderDetailVo orderDetailVo = new orderDetailVo();
            orderDetailVo.setPayOrder(payOrder);
            orderDetailVo.setNum(shopCartVO.getNum());
            orderDetailVo.setPrice(shopCartVO.getPrice());
            orderDetailVo.setProductCode(shopCartVO.getProductCode());
             orderDetailMapper1.inserts(orderDetailVo);
            orderDetailVos.add(orderDetailVo);
            orderVo.setProductCode(shopCartVO.getProductCode());
            BigDecimal price = shopcartMapper.getPrice(orderVo);
         //   BigDecimal price = shopcartMapper.getPrice(shopCartVO.getProductid());
            // 将 Integer 转换为 BigDecimal
            BigDecimal quantity = new BigDecimal(shopCartVO.getNum());
            // 计算总价
            BigDecimal price1 = quantity.multiply(price);
          //  BigDecimal totalPrice = int(shopCartVO.getNum()) * price;
           // orderDetailVo.getProductid(shopCartVO.)//shopCartVO.getNumber();
           totalPrice = totalPrice.add(price1);
        }
    //    orderVo.setDate(LocalDate.now());
     //orderVo.setCreatetime(LocalDateTime.now());
       orderVo.setCreateTime(LocalDateTime.now());
        orderVo.setPrice(totalPrice);
        ProductVo detail = productMapper.getDetail(orderVo.getProductCode());
        orderVo.setUrl(detail.getImageurl());
       // detail.getImageurl();
        ordermapper.insertOrder(orderVo);
      shopcartMapper.deleteShop(orderVo.getUserid());
      return payOrder;
    }

    @Override
    public OrderVo getOrder(String orderId) {
      //  QueryWrapper<OrderVo> queryWrapper = new QueryWrapper();
       // queryWrapper.eq("payorderid", orderId);
       OrderVo result= ordermapper.getOrderByPayOrder(orderId);
       return result;
    }
    public List<OrderVo> getOrders(OrderQueryDto queryDto) {
        return ordermapper.queryOrders(queryDto);
    }

    @Override
    @Transactional
    public void updateOrder(OrderDTO orderVo) {
        ordermapper.updateOrder(orderVo);
    }

    @Override
    public  List<Map<String, Object>> getOrder1(OrderDTO orderVo) {
        List<Map<String, Object>> res=orderMapper.getOrderDetail(orderVo);
        //return ordermapper.getOrder1(orderVo);
        return res;
    }

    @Override
    public OrderVo getOnes(OrderVo orderVo) {
        return ordermapper.selects(orderVo);
    }
}
